package android.pot.com.smartcar.ui.home.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.action.Action;
import android.pot.com.smartcar.base.CommonAdapter;
import android.pot.com.smartcar.base.MyResponseHandler;
import android.pot.com.smartcar.base.swipe.SwipeBackActivity;
import android.pot.com.smartcar.config.MyConfig;
import android.pot.com.smartcar.entity.ProductDetailEntity;
import android.pot.com.smartcar.tool.LoadingTool;
import android.pot.com.smartcar.tool.TipToast;
import android.pot.com.smartcar.ui.home.BGABannerAdapter;
import android.pot.com.smartcar.util.GsonUtil;
import android.pot.com.smartcar.util.TextCheckUtil;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.bingoogolapple.bgabanner.BGABanner;

/**
 * Created by zhaoshilu on 2017/11/27.
 */
public class ProductDetailActivity extends SwipeBackActivity {

    private CommonAdapter adapter;

    private ArrayList<String> imgList = new ArrayList<String>();

    private BGABanner mStackBanner;

    private TextView tv_orignal_price,tv_product_name,tv_price,tv_tip;

    private String productId;

    ProductDetailEntity entity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        initView();
        addListener();
        initData();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mStackBanner!=null){
            mStackBanner.startAutoPlay();
        }

    }

    @Override
    public void onPause() {
        super.onPause();

        if (mStackBanner!=null) {
            mStackBanner.stopAutoPlay();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        getDetailData();
    }

    @Override
    protected void initView() {
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("商品详情");
        tv_product_name =  (TextView) findViewById(R.id.tv_product_name);
        tv_price =  (TextView) findViewById(R.id.tv_price);
        tv_orignal_price = (TextView) findViewById(R.id.tv_orignal_price);
        tv_tip = (TextView) findViewById(R.id.tv_tip);

        tv_orignal_price.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG |Paint.ANTI_ALIAS_FLAG);
        initBannerView();
    }



    private void initBannerView() {
        mStackBanner = (BGABanner) findViewById(R.id.home_kanner);
        mStackBanner.setAdapter(new BGABannerAdapter(this,2));

        mStackBanner.setDelegate(new BGABanner.Delegate<ImageView, String>() {
            @Override
            public void onBannerItemClick(BGABanner banner, ImageView itemView, String model, int position) {
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("img", imgList);
                bundle.putInt("CurPic",position);
                startAct(ProductDetailActivity.this, PhotosScanActivity.class, bundle);
            }
        });
    }


    @Override
    protected void addListener() {
        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToFinishActivity();
            }
        });

        findViewById(R.id.bt_buy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (entity!=null){
                    if (entity.getSold_out_status() == 1){
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("detail",entity);
                        startAct(ProductDetailActivity.this,ProductBuyActivity.class,bundle);
                    } else{
                        TipToast.show("该商品已售罄");
                    }
                }

            }
        });

    }

    @Override
    protected void initData() {
        Bundle bundle =  getIntent().getExtras();
        productId= bundle.getString("productId");
        getDetailData();
    }


    private void getDetailData(){
        Map<String,String> params = new HashMap<>();
        params.put("goods_id",productId+"");
        LoadingTool.StartLoading(this);
        Action.get().goods_details(this, params, new MyResponseHandler() {
            @Override
            public void onSuccess(String message,JSONObject response) {
                LoadingTool.EndLoading(ProductDetailActivity.this);
                entity = GsonUtil.getObject(response.toString(), ProductDetailEntity.class);
                tv_product_name.setText(TextCheckUtil.CheckString(entity.getFull_name()));
                tv_orignal_price.setText("¥ "+TextCheckUtil.CheckString(entity.getOriginal_price()));
                tv_price.setText(TextCheckUtil.CheckString(entity.getSpecial_price()));
                tv_tip.setText(TextCheckUtil.CheckString(entity.getPurchase_notes()));
                String url = MyConfig.IMG_URL + entity.getDetails_map();
                imgList.add(url);
                mStackBanner.setData(imgList,null);
            }
        });
    }

}